package org.itboys.store.manager;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.itboys.store.dto.ProductDTO;
import org.itboys.framework.query.Page;
import org.itboys.store.search.ProductSearchParam;
import org.itboys.store.search.ProductSearchService;
import org.itboys.store.service.StoreProductCategoryService;
import org.itboys.store.service.StoreProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by huml on 2016/4/15.
 * 微网上面的产品业务
 */
@Service
public class OrderProductWeixinService {
    @Autowired
    private StoreProductCategoryService productCategoryService;
    @Autowired
    private StoreProductService productService;
    @Autowired
    private ProductSearchService productSearchService;

    public static final String SORT_LINE = "-";

    /**
     * 分页搜索产品
     * @param catId  分类id
     * @param brandId  品牌id
     * @param keyword  搜索关键字
     * @param sortField  排序方式
     * @return
     */
    public List<ProductDTO> list(Long catId, Long brandId, String keyword, String sortField, Integer type, Integer pageNo, Integer pageSize){
        ProductSearchParam param = new ProductSearchParam();
        if(StringUtils.isNotBlank(keyword)){
            param.setKeyword(keyword);
        }
        if(brandId != null){
            param.setBrandId(brandId.toString());
        }
        if(catId != null){
            param.setCatId(catId.toString());
        }
        if(type != null){
            param.setType(type);
        }
        if(pageNo != null){
            param.setPageNo(pageNo);
        }
        if(pageSize != null){
            param.setPageSize(pageSize);
        }
        if(StringUtils.isNotBlank(sortField)){
            SolrQuery.ORDER sortOrder = SolrQuery.ORDER.asc;
            if(sortField.startsWith(SORT_LINE)){
                sortField = sortField.replace(SORT_LINE, StringUtils.EMPTY);
                sortOrder = SolrQuery.ORDER.desc;
            }
            param.setSortField(sortField);
            param.setSortOrder(sortOrder);
        }
        Page<ProductDTO> page = productSearchService.pageSearchProduct(param);
        return page.getResult();
    }


}
